﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYView;

namespace YJKArch.DimensionModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdPracticeDim : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)111;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			CmdPracticeDim.wndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			Autodesk.Revit.DB.Document document = commandData.Application.ActiveUIDocument.Document;
			Autodesk.Revit.DB.View activeView = commandData.Application.ActiveUIDocument.ActiveView;
			if (!Helper.ViewValidating(document, activeView, ref message))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.InitDimData();
			DlgPracticeDim dlgPracticeDim = new DlgPracticeDim(commandData);
			dlgPracticeDim.Show(CmdPracticeDim.wndRevit);
			int wMsg = 7;
			CmdPracticeDim.SendMessage(CmdPracticeDim.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result;
			for (;;)
			{
				Transaction transaction = new Transaction(document);
				transaction.Start("practiceDim");
				new ElementSet();
				try
				{
					if (new PracticeDimCreator(dlgPracticeDim, commandData).Create(ref message) == null)
					{
						transaction.Commit();
						continue;
					}
					if (dlgPracticeDim != null)
					{
						dlgPracticeDim.Close();
					}
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					message = ex.Message;
					if (dlgPracticeDim != null)
					{
						dlgPracticeDim.Close();
					}
					transaction.RollBack();
					continue;
				}
				break;
			}
			return result;
		}

		private void InitDimData()
		{
			DimDate.FontFamily = AssistFunc.GetSystemFontTypeStringArray(this.m_doc);
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_doc).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements();
			List<string> list = new List<string>();
			foreach (Element element in enumerable)
			{
				Autodesk.Revit.DB.ElementType elementType = (Autodesk.Revit.DB.ElementType)element;
				if ((elementType.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "箭头" || elementType.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "Arrowhead") && elementType.Name != "Arrowhead" && elementType.Name != "箭头" && !list.Contains(elementType.Name))
				{
					list.Add(elementType.Name);
				}
			}
			DimDate.ArrowType = list;
		}

		public static WindowHandle wndRevit;
	}
}
